package com.jxc.wiki.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jxc.wiki.entity.Category;
import com.jxc.wiki.entity.CategoryExample;
import com.jxc.wiki.mapper.CategoryMapper;
import com.jxc.wiki.request.CategoryQueryParamReq;
import com.jxc.wiki.request.CategorySaveParamReq;
import com.jxc.wiki.response.CategoryQueryParamResp;
import com.jxc.wiki.response.PageParamResp;
import com.jxc.wiki.service.CategoryService;
import com.jxc.wiki.util.CopyClassOrListUtil;
import com.jxc.wiki.util.SnowFlakeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    private static final Logger LOG = LoggerFactory.getLogger(CategoryServiceImpl.class);

    @Resource
    private CategoryMapper mapper;

    @Resource
    private SnowFlakeUtil snowFlakeUtil;

    public List<CategoryQueryParamResp> all(){

        CategoryExample categoryExample = new CategoryExample();
        // 按sort字段排序
        categoryExample.setOrderByClause("sort asc");

        List<Category> categoryList = mapper.selectByExample(categoryExample);

        List<CategoryQueryParamResp> categoryRespList = CopyClassOrListUtil.copyList(categoryList, CategoryQueryParamResp.class);

        return categoryRespList;
    }

    // 保存功能,通过插入一条新的数据或者更新一条新的数据实现
    public void save(CategorySaveParamReq req){
        Category category = CopyClassOrListUtil.copy(req,Category.class);
        if (ObjectUtils.isEmpty(req.getId())){

            // 如果id的值为空，则为新增数据,用雪花算法生成id，并设置为category的id
            category.setId(snowFlakeUtil.nextId());
            mapper.insert(category);
        }else {
            // 如果id的值不为空，则为修改数据
            mapper.updateByPrimaryKey(category);
        }
    }

    public void delete(Long id){
        mapper.deleteByPrimaryKey(id);
    }

    public PageParamResp<CategoryQueryParamResp> list(CategoryQueryParamReq req){


        // 实现输入名字查询电子书的功能，用到了模糊查询
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        categoryExample.setOrderByClause("sort asc");
        PageHelper.startPage(req.getPage(),req.getSize());
        List<Category> categoryList = mapper.selectByExample(categoryExample);
        // 打印分页情况日志
        PageInfo<Category> pageInfo = new PageInfo<>(categoryList);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());
//        // 把类型为Category的集合的值拷贝到类型为CategoryResp的集合里
//        List<CategoryResp> categoryRespList = new ArrayList<>();
//        for (Category category : categoryList) {
//            CategoryResp categoryResp = new CategoryResp();
//            // BeanUtils工具类，可以把category的值快速拷贝给categoryResp
//            BeanUtils.copyProperties(category,categoryResp);
//            categoryRespList.add(categoryResp);
//        }

        // 自定义Copy工具类(运用了反射的知识)快速拷贝
        List<CategoryQueryParamResp> categoryRespList = CopyClassOrListUtil.copyList(categoryList, CategoryQueryParamResp.class);

        PageParamResp<CategoryQueryParamResp> pageParamResp = new PageParamResp<>();
        pageParamResp.setTotal(pageInfo.getTotal());
        pageParamResp.setList(categoryRespList);
        return pageParamResp;
    }
}
